// coupon.ts
import { http } from '@/utils/http'
import type {
  CouponInfoVo,
  CustomerCoupon,
  OrderCalculateDTO,
  GoodsDiscountVO,
  CouponReserveDto,
} from './coupon.d'

// 获取优惠券分页列表
export const getCouponList = (params: { current: number; size: number; rangeType: 1 | 2 | 3 }) => {
  return http<PageResult<CouponInfoVo>>({
    url: '/coupon/info/list',
    method: 'GET',
    query: {
      current: params.current,
      size: params.size,
      rangeType: params.rangeType,
    },
  })
}

// 获取用户优惠券分页列表
export const getList = (params: {
  current: number
  size: number
  keyWord: string
  rangeType: 1 | 2 | 3
}) => {
  return http<PageResult<CouponInfoVo>>({
    url: '/customer/coupon/list',
    method: 'GET',
    query: {
      current: params.current,
      size: params.size,
      rangeType: params.rangeType,
      keyWord: params.keyWord,
    },
  })
}

// 领取优惠券
export const receiveCoupon = (
  data: Omit<CustomerCoupon, 'id' | 'isDeleted' | 'receiveTime' | 'useStatus'>,
) => {
  return http<boolean>({
    url: '/customer/coupon/receive',
    method: 'POST',
    data: {
      ...data,
      receiveTime: new Date().toISOString(), // 自动补充领取时间
    },
  })
}
//自动领取优惠卷
export const autoReceiveCoupon = (data: CouponReserveDto) => {
  return http<boolean>({
    url: '/customer/coupon/receive/auto',
    method: 'POST',
    data,
  })
}
/**
 *  计算最优折扣方案
 * @param data
 * @returns
 */
export const calculateBestDiscount = (data: OrderCalculateDTO) => {
  return http<GoodsDiscountVO[]>({
    url: '/coupon/info/calculate',
    method: 'POST',
    data,
  })
}
